<?php
class UserController extends Controller{
	protected $session_user;
	private $topSalle;
	private $topMessage;
	
	public function __construct() {
		$this->topSalle = Loader::loadModel('Salle');
		$this->topSalle = Salle::getTopSalles();
		$this->topMessage = Loader::loadModel('Message');
		$this->topMessage = Message::get10MostLikedMessages();
		
		if($this->isConnected()) {
			$user =Loader::loadModel('User');
			$this->session_user = unserialize(ParameterManager::getInstance()->getSessionVariable('user'));
		}
	}
	
	public function getUserSessionInfos() {
		return $this->session_user;
	}
	
	public function isConnected() {
		if(!is_null(ParameterManager::getInstance()->getSessionVariable('user')))
			return true;
		else
			return false;
	}
	
	public function view($page='homeNotLogged', $param=null) {
		$salle = Loader::loadModel('Salle');
		$topSalles = Salle::getTopSalles();
		$lastSalles;
		
		if($this->isConnected()) {
			$mySalles = Salle::getSallesOfUser($this->session_user->getId());
			if($page=='homeNotLogged')
				$page='homeLogged';
			$connectedUsers = User::getConnectedUsers();
			$lastSalles = Salle::getLastSalles($this->session_user->getId());
			if($this->session_user->getStatusId()!=1) {
				$view = Loader::loadView('View', array('top'=>'topUserLogged','left'=>'leftColumn','center'=>$page));
			} else {
				$view = Loader::loadView('View', array('top'=>'topAdmin','left'=>'leftColumn','center'=>$page));
			}
			$view->setParam('user',$this->session_user);
			$view->setParam('mySalles',$mySalles);
			$view->setParam('connectedUsers', $connectedUsers);
		}
		else {
			$view = Loader::loadView('View', array('top'=>'topUserNotLogged','left'=>'leftColumnNotLogged','center'=>$page));
			$lastSalles = Salle::getLastPublicSalles();
		}
		
		if(is_array($param)) {
			foreach($param as $key=>$aParam)
				$view->setParam($key,$aParam);
		}
		$view->setParam('topsalles',$this->topSalle);
		$view->setParam('topmessages',$this->topMessage);
		$view->setParam('lastSalles',$lastSalles);
		$view->render();
	}
	
	public function viewAll() {
		if($this->isConnected() && $this->session_user->getStatusId()==1) {
			$users = Loader::loadModel('User');
			$users = User::getAllUsers($this->session_user->getId());
			$this->view('viewUsers',array('allUsers'=>$users));
		}
		else {
			$this->view();
		}
	}
	
	public function login() {
		$mail = ParameterManager::getInstance()->getPost('ADRESSEMAIL');
		$pass = ParameterManager::getInstance()->getPost('PASSWORD');
		if(!$this->isConnected() && isset($mail) && isset($pass)) {
			$this->session_user = Loader::loadModel('User');
			$this->session_user = User::tryLogin($mail, md5($pass));
			
			if(isset($this->session_user) && !$this->session_user->isEmpty()) {
				ParameterManager::getInstance()->setSessionVariable('user', serialize($this->session_user));
				$this->session_user->connexion();
			}
		}
		$this->view();
	}
	
	public function logout() {
		if($this->isConnected() && !is_null(ParameterManager::getInstance()->getPost('buttonForm')))
			unset($_SESSION['user']);
		$this->session_user->deconnexion();
		$this->view();
	}
	
	public function profil() {
		$args = ParameterManager::getInstance()->getParam('args');
		if(is_array($args) && isset($args[0])) {
			$user = Loader::loadModel('User');
			$user = User::getUserInfos($args[0]);
			if(!$user->isEmpty()) {
				if($this->isConnected())
					$this->view('viewProfil',array('profil_user'=>$user,'session_user'=>$this->session_user));
				else
					$this->view('viewProfil',array('profil_user'=>$user));
			}
			else {
				$this->view('errorProfile');
			}
		}
		if(!isset($args) && $this->isConnected()){
			$this->view('modifyProfil',array('user'=>$this->session_user,'formations'=>User::getFormations()));
		} else {
			$this->view('restrictedPage');
		}
	}

	public function defaut() {
		$this->view();
	}
	
	public function signup() {
		$user = Loader::loadModel('User');
		$isPost = ParameterManager::getInstance()->getPost('SIGNUPBUTTON');
		if(isset($isPost)) {
			$post[0] = ParameterManager::getInstance()->getPost('NOM');
			$post[1] = ParameterManager::getInstance()->getPost('PRENOM');
			$post[2] = ParameterManager::getInstance()->getPost('PSEUDO');
			$post[3] = ParameterManager::getInstance()->getPost('ADRESSEMAIL');
			$post[4] = ParameterManager::getInstance()->getPost('PASSWORD');
			$post[5] = 	ParameterManager::getInstance()->getPost('ANNEE').'-'.
						ParameterManager::getInstance()->getPost('MOIS').'-'.
						ParameterManager::getInstance()->getPost('JOUR');
			$post[6] = ParameterManager::getInstance()->getPost('PROMOTION');
			$post[7] = ParameterManager::getInstance()->getPost('FORMATION');

			User::createUser($post[0],$post[1],$post[2],md5($post[4]),$post[3],$post[6],$post[7],$post[5]);
			$this->view('accountCreated');
		}
		$this->view('signup', array('formations'=>User::getFormations()));
		
	}
	
	public function checkpseudo() {
		$pseudo = ParameterManager::getInstance()->getQuery('pseudo');
		Loader::loadModel('User'); 
		echo (User::checkPseudo($pseudo));
	}
	
	public function checkmail() {
		$mail = ParameterManager::getInstance()->getQuery('mail');
		Loader::loadModel('User'); 
		echo (User::checkMail($mail));
	}
	
	public function modifyProfil() {
		$pseudo = ParameterManager::getInstance()->getQuery('pseudo');
		$pass = ParameterManager::getInstance()->getQuery('pass');
		$birthdate = ParameterManager::getInstance()->getQuery('birthdate');
		$promo = ParameterManager::getInstance()->getQuery('promo');
		$formation = ParameterManager::getInstance()->getQuery('formation');
		if($this->isConnected()) {
			Loader::loadModel("User");
			
			if(!empty($pseudo)) {
				$this->session_user->modifierPseudo($pseudo);
			}
			if(!empty($pass)) {
				$this->session_user->modifierPass($pass);
			}
			if(!empty($birthdate)) {
				$this->session_user->modifierDateNaissance($birthdate);
			}
			if(!empty($promo)) {
				$this->session_user->modifierPromo($promo);
			}
			if(!empty($formation)) {
				$this->session_user->modifierFormation($formation);
			}
		}
	}
	
	public function uploadPhoto() {
		$photo = ParameterManager::getInstance()->getPost('PHOTO');
		require_once __CLASS_PATH.'Image.class.php';
		//if(!empty($photo)) {
			if(isset($_FILES['PHOTO']['name'])) {
				$image = new Image($_FILES['PHOTO']['name']);
				echo $_FILES['PHOTO']['name'];
				$image->resize_to(200, 200);
				$image->save_as(__PHOTOS_PATH.$this->session_user->getId().'.jpg');
			}
		//}
	}
	
	public function favoris() {
		if(!empty($this->session_user)) {
			$favoris = Loader::loadModel('Message');
			$favoris = Message::getMyLikes($this->session_user->getId());
			$this->view('favoris',array('favoris'=>$favoris));
		} else {
			$this->view('restrictedPage');
		}
	}
	
	public function stats() {
		$nbUsers = Loader::loadModel('User');
		$nbUsers = User::getNumberOfUsers();
		$nbConnectedUsers = User::getNumberOfConnectedUsers();
		
		$nbSalles = Loader::loadModel('Salle');
		$nbSalles = Salle::getNumberOfSalles();
		$nbPublic = Salle::getNumberOfPublicSalles();
		$nbPrive = Salle::getNumberOfPrivateSalles();
		
		
		$nbMessages = Loader::loadModel('Message');
		$nbMessages = Message::getNumberOfMessages();
		$lastPost = Message::getDateLastPost();
		
		$nbMP = Loader::loadModel('MessagePrives');
		$nbMP = MessagePrives::getNumberOfPrivateMessages();
		
		$this->view('stats',array('nbUsers'=>$nbUsers,'nbConnectedUsers'=>$nbConnectedUsers,
									'nbSalles'=>$nbSalles,'nbPublic'=>$nbPublic,'nbPrive'=>$nbPrive,
									'nbMessages'=>$nbMessages, 'lastPost'=>$lastPost,'nbMP'=>$nbMP));
	}
	
}